<?php
class MasterTableSeeder extends Seeder {

    public function run()
    {
        // to use non Eloquent-functions we need to unguard
        Eloquent::unguard();

        // All existing users are deleted !!!
        DB::table('users')->delete();
        DB::table('roles')->delete();
        DB::table('category')->delete();
		
        DB::table('department_permission')->delete();
        DB::table('department_template')->delete();
        DB::table('permissions')->delete();
        DB::table('departments')->delete();
		
        DB::table('document_index')->delete();
        DB::table('documents')->delete();
		
        DB::table('templateParams')->delete();
        DB::table('templates')->delete();
		
		// add roles using Eloquent
        $role = new Role;
        $role->role_name = 'Administrator';
        $role->save();
        $role = new Role;
        $role->role_name = 'User';
        $role->save();
		
        $permission_printable = new Permission;
        $permission_printable->permission_key = 'PRINTABLE';
        $permission_printable->save();
        $permission_editable = new Permission;
        $permission_editable->permission_key = 'EDITABLE';
        $permission_editable->save();
		
		$array = array();
		$array = array_add($array, $permission_printable->id, $permission_printable->id);
		$array = array_add($array, $permission_editable->id, $permission_editable->id);
		
        $department1 = new Department;
        $department1->short_department_name = 'ER';
        $department1->department_name = 'EMERGENCY ROOM';
        $department1->department_description = 'ห้องอุบัติเหตุและฉุกเฉิน';
        $department1->pdf_password = 'emergency';
        $department1->save();
		$department1->permissions()->attach($permission_printable);
		$department1->permissions()->attach($permission_editable);
		
        $department2 = new Department;
        $department2->short_department_name = 'OR';
        $department2->department_name = 'OPERATING ROOM';
        $department2->department_description = 'ห้องผ่าตัด';
        $department2->pdf_password = 'operating';
        $department2->save();				
		$department2->permissions()->sync(array($permission_printable->id));
		$department2->permissions()->sync(array($permission_editable->id));
		
        $department3 = new Department;
        $department3->short_department_name = 'LR';
        $department3->department_name = 'LABOR ROOM';
        $department3->department_description = 'ห้องคลอด';
        $department3->pdf_password = 'labor';
        $department3->save();		
		$department3->permissions()->save($permission_printable);
		$department3->permissions()->save($permission_editable);
		
		for ( $y = 2 ; $y <= 10 ; $y++ ) {
			$template = new Template;
			$template->template_name = 'template#'.$y;
			$template->save();
			
			for ( $x = 1 ; $x <= 4 ; $x++ ) {
				$templateParam = new TemplateParam;
				$templateParam->template_param_name = 'param#'.$x;
				$templateParam->template_param_type = 'STRING';
				$template->templateParams()->save($templateParam);
			}
					
			$department3->templates()->save($template);
		}
		
		for ( $y = 11 ; $y <= 20 ; $y++ ) {
			$template = new Template;
			$template->template_name = 'template#'.$y;
			$template->save();
			
			for ( $x = 1 ; $x <= 5 ; $x++ ) {
				$templateParam = new TemplateParam;
				$templateParam->template_param_name = 'param#'.$x;
				$templateParam->template_param_type = 'STRING';
				$template->templateParams()->save($templateParam);
			}
					
			$department2->templates()->save($template);
		}
		
		for ( $y = 21 ; $y <= 30 ; $y++ ) {
			$template = new Template;
			$template->template_name = 'template#'.$y;
			$template->save();
			
			for ( $x = 1 ; $x <= 6 ; $x++ ) {
				$templateParam = new TemplateParam;
				$templateParam->template_param_name = 'param#'.$x;
				$templateParam->template_param_type = 'STRING';
				$template->templateParams()->save($templateParam);
			}
					
			$department1->templates()->save($template);
		}
		
		$img_path = "C:temp/V0_D2553-03-01_H0102925.tif";
		
		$document = new Document;
		$document->create_by = 'Admin';
		$document->update_by = 'Admin';
		$document->content = File::get($img_path);
		$document->template_id = $template->id;
        $document->save();
		
		foreach ($template->templateParams as $key => $templateParam)
		{
			$documentIndex = new DocumentIndex;
			$documentIndex->document_id = $document->id;
			$documentIndex->template_param_id = $templateParam->id;
			$documentIndex->index_value = 'value#'.$key;
			
			$document->documentIndex()->save($documentIndex);
		}
		
		$catRoot = new Category;
		$catRoot->name = '/';
		$catRoot->save();
		
		$catElectronics = new Category;
		$catElectronics->name = 'ELECTRONICS';
		$catElectronics->parent_id = $catRoot->id;
		$catElectronics->save();
		
		$catTELEVISIONS = new Category;
		$catTELEVISIONS->name = 'TELEVISIONS';
		$catTELEVISIONS->parent_id = $catElectronics->id;
		$catTELEVISIONS->save();
		
		$catTUBE = new Category;
		$catTUBE->name = 'TUBE';
		$catTUBE->parent_id = $catTELEVISIONS->id;
		$catTUBE->save();
		$catLCD = new Category;
		$catLCD->name = 'LCD';
		$catLCD->parent_id = $catTELEVISIONS->id;
		$catLCD->save();
		$catPLASMA = new Category;
		$catPLASMA->name = 'PLASMA';
		$catPLASMA->parent_id = $catTELEVISIONS->id;
		$catPLASMA->save();
		
		$catPORTABLE = new Category;
		$catPORTABLE->name = 'PORTABLE ELECTRONICS';
		$catPORTABLE->parent_id = $catElectronics->id;
		$catPORTABLE->save();
		
		$catMP3 = new Category;
		$catMP3->name = 'MP3 PLAYERS';
		$catMP3->parent_id = $catPORTABLE->id;
		$catMP3->save();
		
		$catFLASH = new Category;
		$catFLASH->name = 'FLASH';
		$catFLASH->parent_id = $catMP3->id;
		$catFLASH->save();
		
		$catCD = new Category;
		$catCD->name = 'CD PLAYERS';
		$catCD->parent_id = $catPORTABLE->id;
		$catCD->save();
		
		$cat2WAY = new Category;
		$cat2WAY->name = '2 WAY RADIOS';
		$cat2WAY->parent_id = $catPORTABLE->id;
		$cat2WAY->save();
		
		// foreach($department->permissions as $permissionItem) {
			// $this->command->info('User - Permission >> ' . $permissionItem->id);
			// $this->command->info('User - Permission >> ' . $permissionItem->permission_key);
		// }
    }
}